package com.black.web.controller.system;

import com.alibaba.excel.EasyExcel;
import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import com.black.system.domain.req.system.add.SysDictTypeAddReq;
import com.black.system.domain.req.system.edit.SysDictTypeEditReq;
import com.black.system.domain.req.system.query.SysDictTypeQueryReq;
import com.black.system.domain.resp.system.SysDictTypeResp;
import com.black.system.service.SysDictTypeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author L
 */
@Api(tags = "后台-字典类型Api")
@RestController
@RequestMapping("/admin/dict/type")
public class SysDictTypeController extends BaseController {
    //字典类型管理
    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public R<PageInfo<SysDictTypeResp>> list(SysDictTypeQueryReq req) {
        PageInfo<SysDictTypeResp> page = this.sysDictTypeService.paginationQuery(req);
        return R.ok(page);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public R<SysDictTypeResp> findById(@PathVariable(value = "id") Long id) {
        SysDictTypeResp sysDictTypeResp = this.sysDictTypeService.findById(id);
        return R.ok(sysDictTypeResp);
    }

    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @ApiOperation(value = "添加")
    @PostMapping
    public R add(@RequestBody @Valid SysDictTypeAddReq req) {
        boolean res = this.sysDictTypeService.add(req);
        return super.isSuccess(res);
    }

    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @ApiOperation(value = "修改")
    @PutMapping
    public R edit(@RequestBody @Valid SysDictTypeEditReq req) {
        boolean res = this.sysDictTypeService.edit(req);
        return super.isSuccess(res);
    }

    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids) {
        boolean res = this.sysDictTypeService.deleteById(ids);
        return super.isSuccess(res);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @ApiOperation(value = "导出到Excel")
    @PostMapping("/export")
    public void download(HttpServletResponse response, SysDictTypeQueryReq req, Long[] ids) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        // .xlsx 文件的  mine-type类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 内容响应编码
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("dicttype", "UTF-8").replaceAll("\\+", "%20");
        // 告诉浏览器，文件名称叫什么
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 查询数据库，向EasyExcel写入数据
        List<SysDictTypeResp> data = this.sysDictTypeService.export(req, ids);
        EasyExcel.write(response.getOutputStream(), SysDictTypeResp.class).sheet().doWrite(data);
    }

}
